const util = require("../../../utils/util.js");

var app = getApp();
var that;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    newOrder: "",
    time: "",
    date: "",
    productImg: {},
    userName: "sweetstreetUser",
    avatarUrl: "http://static.cakeboss.com.cn/wx-tian-headerIcon.svg",
    couponNumStr: 0,
    rechargePrice: 0,
    phoneStr: "",
    showGetPhone:false,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.$wuxToast = app.wux(this).$wuxToast;
    that = this;
    console.log(app.globalData.phone, "app.globalData.phone++++++")
    console.log(that.data.phoneStr, "that.data.phoneStr++++++")
    if (app.globalData.nickName != "" && app.globalData.nickName != undefined) {
      that.setData({
        avatarUrl: app.globalData.userInfo.avatarUrl,
        userName: app.globalData.nickName,
      });
    }
    this.setData({
      time: app.globalData.time,
      date: app.globalData.date,
      productImg: {},
    })
  },
  onShow: function(options) {
    console.log("页面出现");
    this.setData({
      phoneStr: app.globalData.phone
    })
    console.log(this.getTabBar, "this.getTabBar")
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 1 //tabBar第二个选中
      })
    }
    console.log(this.getTabBar().data.selected, "this.getTabBar().data.selected")
    this.setData({
      time: app.globalData.time,
      date: app.globalData.date
    });
    console.log("页面出现time===" + this.data.time);
    console.log("页面出现date===" + this.data.date);
    // 检查openId
    util.checkOpenId(that);
  },
  /**
   * 我的优惠券
   */
  myCouponTapAction: function(e) {
    wx.showToast({
      title: '暂时未开通！',
      icon:"none",
    })
    return;
    /*
     * 若本地没有正确的手机号则显示登录/注册弹窗，
     * 若有正确的手机号则直接跳转页面
     */
    if (!app.globalData.viewId) {
      that.setData({
        showGetPhone: true
      })
    }else{
      wx.navigateTo({
        url: '/pages/coupon/list/coupon',
      })
    }
  },
  /**
   * 我的订单
   */
  myOrderAction: function(e) {
    /*
     * 若本地没有正确的手机号则显示登录/注册弹窗，
     * 若有正确的手机号则直接跳转页面
     */
    if(!app.globalData.viewId){
      that.setData({
        showGetPhone:true
      })
    }else{
      wx.navigateTo({
        url: '/pages/order/orderList/orderList',
      });
    }
  },
  /**
   * 我的地址
   */
  myAddressAction: function(e) {
    /*
     * 若本地没有正确的手机号则显示登录/注册弹窗，
     * 若有正确的手机号则直接跳转页面
     */
    if (!app.globalData.viewId) {
      that.setData({
        showGetPhone: true
      })
    }else{
      wx.navigateTo({
        url: '/pages/address/list/list',
      })
    }
  },
  // 绑定手机号
  bindUserPhone: function (e) {
    util.getUserPhone(e, res => {
      that.setData({
        phoneStr: res.data.data.phone,
        showGetPhone: false
      })
      app.globalData.phone = res.data.data.phone;
    });
  },
  // getPhoneNumber: function(e) {
  //   console.log(e.detail.errMsg)
  //   console.log(e.detail.iv)
  //   console.log(e.detail.encryptedData)
  //   console.log(app.globalData.openId, "app.globalData.openId")
  //   if (e.detail.errMsg === "getPhoneNumber:ok") { //用户拒绝授权
  //     console.log(111)
  //     wx.request({
  //       url: app.globalData.BaseURL + '/api/wx/bindPhone',
  //       method: 'GET',
  //       header: {
  //         "Content-Type": "application/x-www-form-urlencoded"
  //       },
  //       data: {
  //         openId: app.globalData.openId,
  //         sessionKey: app.globalData.sessionKey,
  //         encryptedData: e.detail.encryptedData,
  //         iv: e.detail.iv,
  //       },
  //       success: function(res) {
  //         var code = res.data.code;
  //         if (code == 200) {
  //           wx.showToast({
  //             title: res.data.msg,
  //           })
  //           app.globalData.viewId = res.data.data.viewId;
  //           app.globalData.phone = res.data.data.phone;
  //           console.log(app.globalData.phone, "app.globalData.phone+++")
  //           that.setData({
  //             phoneStr: res.data.data.phone
  //           })
  //           console.log(that.data.phoneStr,"phoneStr")
  //         } else {
  //           that.$wuxToast.show({
  //             type: 'text',
  //             timer: 1500,
  //             color: '#fff',
  //             text: res.data.msg,
  //             success: () => console.log(res.data.msg)
  //           })
  //         }
  //       },
  //       fail: function() {
  //         that.$wuxToast.show({
  //           type: 'text',
  //           timer: 1500,
  //           color: '#fff',
  //           text: '请求失败',
  //           success: () => console.log('请求失败')
  //         })
  //       }
  //     });
  //   }
  // },
  /**
   * 储值
   */
  // myRechargeAction: function () {

  //   var phoneStr = util.validatePhone(app.globalData.phone);
  //   /*
  //    * 若本地没有正确的手机号则显示登录/注册弹窗，
  //    * 若有正确的手机号则直接跳转页面
  //    */
  //   if (!that.data.phoneStr) {
  //   } else {
  //     wx.navigateTo({
  //       url: '../storedCard/storedCard?rechargePrice=' + that.data.rechargePrice,
  //     })
  //   }
  // },
  /**
   * myActivityAction
   */
  myActivityAction: function() {
    wx.navigateTo({
      // url: '../../activitys/bdActivity/bdActivity',
      url: '../../activitys/beginActivity/beginActivity',
    })
  },
  // 请求全部代金券
  getCouponList: function() {
    var openId = app.globalData.openId
    wx.request({
      url: app.globalData.BaseURL + '/miniprogram/usercoupon' + app.globalData.accessToken + '&phone=' + app.globalData.phone + '&price=10000000',
      method: 'GET',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: '',
      success: function(res) {
        var code = res.statusCode;
        if (code == 200) {
          var results = res.data;
          console.log("代金券列表：" + results);
          that.setData({
            couponNumStr: results.length
          });

        } else {
          that.$wuxToast.show({
            type: 'text',
            timer: 1500,
            color: '#fff',
            text: '请求错误',
            success: () => console.log('请求错误')
          })
        }
      },
      fail: function() {
        that.$wuxToast.show({
          type: 'text',
          timer: 1500,
          color: '#fff',
          text: '请求失败',
          success: () => console.log('请求失败')
        })
      }
    });
  },
  // 获取我的余额 
  // getRechargePrice: function () {
  //   var openId = app.globalData.openId;
  //   var phone = app.globalData.phone;
  //   wx.request({
  //     url: app.globalData.BaseURL + '/miniprogram/svc/balance' + app.globalData.accessToken + '&openid=' + openId + '&phone=' + phone,
  //     method: 'GET',
  //     header: {
  //       "Content-Type": "application/x-www-form-urlencoded"
  //     },
  //     data: '',
  //     success: function (res) {
  //       var code = res.statusCode;
  //       if (code == 200) {
  //         console.log("我的余额：" + res.data.data.recordValue);
  //         that.setData({
  //           rechargePrice: res.data.data.recordValue
  //         });

  //       } else {
  //         that.$wuxToast.show({
  //           type: 'text',
  //           timer: 1500,
  //           color: '#fff',
  //           text: res.data.msg,
  //           success: () => console.log(res.data.msg)
  //         })
  //       }
  //     },
  //     fail: function () {
  //       that.$wuxToast.show({
  //         type: 'text',
  //         timer: 1500,
  //         color: '#fff',
  //         text: '请求失败',
  //         success: () => console.log('请求失败')
  //       })
  //     }
  //   });
  // },
  // 获取个人信息
  getUserInfoAction: function(res) {
    console.log(res, "res")
    if (res.detail.errMsg == "getUserInfo:ok") {
      that.setData({
        userName: res.detail.userInfo.nickName,
        avatarUrl: res.detail.userInfo.avatarUrl,
      })
      app.globalData.nickName = res.detail.userInfo.nickName;
      app.globalData.avatarUrl = res.detail.userInfo.avatarUrl;
      app.globalData.userInfo = res.detail.userInfo;
    }
  },
})